<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const info = {
      name: 'why',
      age: 18,
      friend: {
        name: 'kobe'
      },
      running: function () { },
      [Symbol()]: 'abc',
    }
    info.obj = info

    // 1.操作一:引用赋值
    const obj1 = info

    // 2.操作二:浅拷贝
    const obj2 = { ...info }
    obj2.name = 'james'
    obj2.friend.name = 'jordan'
    console.log(info.name) // why (没有改变)
    console.log(info.friend.name) // jordan (被改变了)

    const obj3 = Object.assign({}, info)
    obj3.name = 'curry'
    obj3.friend.name = 'wade'
    console.log(info.name) // why (没有改变)
    console.log(info.friend.name) // wade (被改变了)

    // 3.操作三:深拷贝
    // 3.1 JSON方法
    // 3.1.1不能拷贝函数
    // 3.1.2不能拷贝symbol
    // 3.1.3不能拷贝循环引用
    const obj4 = JSON.parse(JSON.stringify(info)) 
    obj4.name = 'bob'
    obj4.friend.name = 'alice'
    console.log(info.name) // why (没有改变)
    console.log(info.friend.name) // wade (没有改变)
    console.log(obj4.name) // bob
    console.log(obj4.friend.name) // alice
    console.log(obj4.running) // undefined
    console.log(obj4) // {name: "bob", age: 18, friend: {…}}

    // 3.2 自己编写一个深拷贝函数(第三方库)
  </script>
</body>

</html>